
quietly {

clear all

********************************************************************************
***   1. Append Data 
********************************************************************************

run "do files/senegal r2 cleaning.do"
tempfile r2
save `r2'

run "do files/senegal r3 cleaning.do"
tempfile r3
save `r3'

run "do files/senegal r4 cleaning.do"
tempfile r4
save `r4'

run "do files/senegal r5 cleaning.do"
tempfile r5
save `r5'

run "do files/senegal r6 cleaning.do"

append using `r2'
append using `r3'
append using `r4'
append using `r5'

merge 1:1 round respno using "data files/ab geocoding.dta" /* merge census adm */

order round respno RegionName-CRCode
drop _merge



********************************************************************************
***   2. Add Intensity
********************************************************************************


preserve
use "data files/intensity data.dta", clear
replace CACode = 142100 if CACode == 141100
replace CACode = 142200 if CACode == 141200
replace CRCode = _n if CRCode == .
tempfile tomerge
save `tomerge'
restore


merge m:1 CACode using `tomerge', keepusing(pre_10_2002 - comp_female_alt population) update
drop if _merge == 2
drop _merge

merge m:1 CRCode using `tomerge', keepusing(pre_10_2002 - comp_female_alt) update
drop if _merge == 2
drop _merge

gen int_male = inc_male/(inc_male + comp_male)
gen int_female = inc_female/(inc_female + comp_female)
gen int_overall = (inc_male + inc_female)/(inc_male + comp_male + inc_female + comp_female)

gen int_male_alt = inc_male_alt/(inc_male_alt + comp_male_alt)
gen int_female_alt = inc_female_alt/(inc_female_alt + comp_female_alt)
gen int_overall_alt = (inc_male_alt + inc_female_alt)/(inc_male_alt + comp_male_alt + inc_female_alt + comp_female_alt)

sort round respno


********************************************************************************
***   3. Create Outcome Indices
********************************************************************************

* Economic

foreach var of varlist no_food no_water no_healthcare no_fuel no_income {
	recode `var' (0=1) (1=0)
}

alpha no_food no_water no_healthcare no_fuel no_income, gen(basic_necessities)

gen employed = 1 if employment_status != ""
replace employed = 0 if employment_status == "unemployed" | employment_status == "inactive"

alpha owns_radio owns_tv owns_vehicle, gen(assets)
alpha basic_necessities employed assets, gen(economic_index) 


* Information

alpha radio tv newspaper, gen(news_scale)
alpha radio_news_often tv_news_often newspaper_news_often, gen(news_often) 
alpha news_scale interest_pub_affairs_index discuss_pol_index, gen(information)
alpha news_often interest_pub_affairs_index discuss_pol_index, gen(information2)


* Participation

alpha contact_officials_index contact_LG_councilor_index contact_MP_index, gen(contact_index)
alpha vote1 raise_issue_index, gen(participate_index)
alpha participate_index contact_index, gen(participation_index)



* Democratic Institutions

gen media_accountability_index = media_accountability
replace media_accountability_index = 2 if media_accountability_strong == 1

gen against_pres_discretion_index = against_president_discretion
replace against_pres_discretion_index = 2 if against_pres_discr_strong == 1

gen checks_balances_index = checks_balances
replace checks_balances_index = 2 if checks_balances_strong == 1

gen against_gov_ban_org_index = against_govt_ban_org
replace against_gov_ban_org_index = 2 if against_govt_ban_org_str == 1

gen opp_parties_crit_index = opp_parties_criticize
replace opp_parties_crit_index = 2 if opp_parties_crit_str == 1

gen rej_oneman_rule_index = reject_one_man_rule
replace rej_oneman_rule_index = 2 if r_one_man_r_strongly == 1

gen rej_mil_rule_index = reject_military_rule
replace rej_mil_rule_index = 2 if r_mil_rule_str == 1

gen against_oneparty_rule_index = against_one_party_rule
replace against_oneparty_rule_index = 2 if a_one_party_rule_strong == 1

gen term_limits_index = term_limits
replace term_limits_index = 2 if term_limits_strongly == 1

gen elec_choice_index = election_choice_leaders
replace elec_choice_index = 2 if election_choice_leaders_str == 1

gen newspaper_free_index = newspaper_free
replace newspaper_free_index = 2 if newspaper_free_str == 1

alpha trust_courts trust_EC careful_say free_to_speak_mind free_to_join_pol_org free_vote_choice how_democratic satisfaction_democracy, gen(dem_gov)
alpha rej_oneman_rule_index term_limits_index against_pres_discretion_index opp_parties_crit_index checks_balances_index, gen(dem_checks_on_pres)
alpha media_accountability_index against_oneparty_rule_index rej_mil_rule_index elec_choice_index newspaper_free_index against_gov_ban_org_index supports_democracy, gen(dem_institutions) 
alpha dem_checks_on_pres dem_institutions, gen(dem_support)


* Non-democracy related

alpha corruption_president corruption_government_officials corruption_MP corruption_LG_councilors, gen(corruption)
alpha performance_president performance_mp performance_LG_councilor, gen(performance)
alpha handles_economy handles_employment handles_inflation handles_inequality, gen(economy)
alpha handles_health handles_education handles_water loc_gov_handles_roads loc_gov_handles_clean loc_gov_handles_markets loc_gov_handles_health loc_gov_handles_fees, gen(pub_goods)

alpha performance economy pub_goods, gen(performance_index)


********************************************************************************
***   4. Treatment and Controls
********************************************************************************


* admin_group
gen locale_3_control = CRCode
replace locale_3_control = CACode if CRCode == .

* Birthyear
replace age = . if age == -1
gen birthyear_control = year_survey - age

* Survey year
ren year_survey year_survey_control

* Gender 
replace gender = "Female" if gender == "Femme"
replace gender = "Male" if gender == "Homme"
gen male = 0
replace male = 1 if gender == "Male"

* Urban
tab urbrur, gen(urbrur)


* Intensity vars

gen treat2_10_inc = int_male if gender == "Male"
replace treat2_10_inc = int_female if gender == "Female"

gen treat2_inc = int_male_alt if gender == "Male"
replace treat2_inc = int_female_alt if gender == "Female"

foreach var of varlist treat* {
	egen med_`var' = median(`var')
}


* Locale-gender 
egen locale_gender = group(locale_3_control male)


* Post vars

gen post1 = 0
replace post1 = 0.25 if birthyear + 16 >= 2002 & age != .
replace post1 = 0.5 if birthyear + 15 >= 2002 & age != .
replace post1 = 0.75 if birthyear + 14 >= 2002 & age != .
replace post1 = 1 if birthyear + 13 >= 2002 & age != .

gen post2 = post1
replace post2 = . if post1 != 0 & post1 != 1


* Instrument
gen instrument = post1*treat2_10_inc
egen instrument_std = std(instrument)
drop instrument
ren instrument_std instrument
label var instrument "Post $\times$ Intensity"


* Schooling
gen school = .
replace school = 1 if no_school == 1
replace school = 2 if informal_school == 1
replace school = 3 if primary_school_inc == 1
replace school = 4 if primary_school == 1
replace school = 5 if secondary_school_inc == 1
replace school = 6 if secondary_school == 1
replace school = 7 if college == 1

gen school2 = .
replace school2 = 1 if no_school == 1 | informal_school == 1
replace school2 = 2 if primary_school_inc == 1
replace school2 = 3 if primary_school == 1
replace school2 = 4 if secondary_school_inc == 1 | secondary_school == 1 | college == 1

gen school3 = .
replace school3 = 1 if no_school == 1 | informal_school == 1
replace school3 = 2 if primary_school_inc == 1
replace school3 = 3 if primary_school == 1
replace school3 = 4 if secondary_school_inc == 1
replace school3 = 5 if secondary_school == 1 | college == 1

gen school4 = .
replace school4 = school2
replace school4 = 3 if school2 == 4
recode school4 (1=0) (2=1) (3=2)

label var school "School (1-7)"
label var school2 "School (1-4)"
label var school3 "School (1-5)"
label var school4 "School (1-3)"

gen prim_comp_only = primary_school
replace primary_school = 0 if secondary_school_inc == 1 | secondary_school == 1 | college == 1

gen sec_school_inc_only = secondary_school_inc
replace sec_school_inc_only = 0 if secondary_school == 1 | college == 1

gen sec_comp_only = secondary_school
replace sec_comp_only = 0 if college == 1


* heterogeneous effects years

gen W = year==2005 | year==2008
label var W "HE"
}


est clear

gen indiv2 = school4
replace indiv2 = 1 if school4 == 2

gen indiv3 = school4
recode indiv3 (1=0)(2=1)

gen indiv4 = school
recode indiv4 (1/4 = 0) (5/7 = 1)

label var school4 "Schooling"
label var indiv2 "Primary incomplete"
label var indiv3 "Primary complete"
label var indiv4 "Secondary incomplete"
